package com.xmkj.bearuser.count;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.common.Entity.WechatLoginEntity;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.bearusermethod.BindPhoneAndLoginMethods;
import com.common.retrofit.methods.GetCodeMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.RegexUtils;
import com.common.utils.SPUtils;
import com.xmkj.bearuser.MainBearBizActivity;
import com.xmkj.bearuser.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/7/27
 * 描述：
 */

public class BindBearPhoneActivity extends BaseMvpActivity {
    public static final String USERBEAN = "userbean";
    @BindView(R.id.tv_flag)
    TextView tvFlag;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_flag1)
    TextView tvFlag1;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.tv_flag2)
    TextView tvFlag2;
    @BindView(R.id.et_invide_code)
    EditText etInvideCode;
    @BindView(R.id.tv_next_step)
    TextView tvNextStep;
    private TimeCount timeCount;
    private WechatLoginEntity userBean;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        userBean = (WechatLoginEntity) getIntent().getSerializableExtra(USERBEAN);
        SPUtils.setShareString(Constans.HASHID,userBean.hashid);
        SPUtils.setShareInt(Constans.UID,userBean.uid);
        timeCount = new TimeCount(60000, 1000);//构造CountDownTimer对象
        attachClickListener(tvGetCode);
        attachClickListener(tvNextStep);
    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == tvGetCode.getId()) {
            if (EmptyUtils.isEmpty(getEditTextStr(etPhone))) {
                showToastMsg("请输入手机号");
                return;
            } else if (!RegexUtils.isMobileExact(getEditTextStr(etPhone))) {
                showToastMsg("手机号不正确");
                return;
            }
            getCode();
        } else if (view.getId() == tvNextStep.getId()) {
            if (EmptyUtils.isEmpty(getEditTextStr(etCode))) {
                showToastMsg("请输入验证码");
                return;
            } else if (EmptyUtils.isEmpty(getEditTextStr(etInvideCode))) {
                showToastMsg("请输入邀请码");
                return;
            }
//            gotoCheck();
            gotoLogin();
        }
    }

    private void getCode() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                showToastMsg("短信发送成功");
                timeCount.start();
            }

            @Override
            public void onError(String e, int code) {
                SPUtils.setShareString(Constans.HASHID,"");
                dismissProgressDialog();
                showToastMsg(e);
            }
        });


        GetCodeMethods.getInstance().getCode(subscriber, getEditTextStr(etPhone), "4");
        rxManager.add(subscriber);
    }

    private void gotoLogin() {

        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                showToastMsg("绑定成功");
                SPUtils.setShareString(Constans.HASHID,userBean.hashid);
                SPUtils.setShareInt(Constans.UID,userBean.uid);
                SPUtils.setShareString(Constans.INVIDECODE,getEditTextStr(etInvideCode));
                gotoActivity(MainBearBizActivity.class);
                finish();

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });


        BindPhoneAndLoginMethods.getInstance().bindMobile(subscriber, userBean.uid+"",userBean.hashid,getEditTextStr(etPhone),getEditTextStr(etCode),getEditTextStr(etInvideCode));
        rxManager.add(subscriber);
    }
    /**
     * 计时器
     * millisInFuture  总时长
     * countDownInterval 计时间隔
     */
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvGetCode.setClickable(false);
            tvGetCode.setText(millisUntilFinished / 1000 + "  秒");
        }

        @Override
        public void onFinish() {
//            btn_code.setBackgroundResource(R.drawable.btn_code);
            tvGetCode.setText("重新发送");
            tvGetCode.setClickable(true);
        }
    }
    @Override
    protected void setNavigation() {
        setNavigationBack("绑定手机页");
    }



    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
